<!--
* description: 回收网点入库信息新增
* author: hzm
* createdDate: 2021-4-29
-->
<template>
  <section class="editBigAreaInfo">
    <el-dialog
      v-dialogDrag
      :close-on-click-modal="false"
      :title="textMap[popupsState]"
      :append-to-body="true"
      :visible.sync="curPopupsVisible"
      width="70%"
      @close="closeEdit"
      @open="open"
    >
      <dialogHeader
        slot="title"
        :title="textMap[popupsState]"
        :style-type="$store.state.app.dialogType"
      />

      <div class="filter-container filter-params">
        <el-row ref="vailComponentList" :gutter="26">
          <component
            :is="comp.component"
            v-for="comp in tableComponents.filter(o => o.isMust === true)"
            :ref=" comp.isRequire ? comp.isRequire + comp.compKey : comp.compKey"
            :key="comp.compKey"
            :validrule="comp.validrule"
            :code-field="comp.codeField"
            :text-field="comp.textField"
            :popups-key="comp.compKey"
            :parent-fileds="comp.parentFileds"
            :code="formField[comp.codeField]"
            :text="formField[comp.textField]"
            :disabled="comp.disabled"
            :o-fields="comp.oFields"
            :options="comp.options"
            :show-placeholder="comp.placeholder"
            :custom-obj="comp.customObj"
            :is-require="comp.isRequire"
            :is-mul="comp.isMul"
            :span="comp.span || 24"
            :label-name="comp.labelName"
            :lookuptype="comp.lookuptype"
            :data-to-object="comp.dataToObject"
            :date-options-type="comp.dateOptionsType"
            @changeCode="getComponentCode1"
            @focusCode="getFocusCode"
          />
        </el-row>
      </div>

      <div slot="footer" class="dialog-footer">
        <el-button
          v-for="comp in tableButtons"
          :key="comp.compKey"
          :type="comp.type"
          :icon="comp.icon"
          @click="comp.clickEvent"
        >{{ comp.text }}</el-button>
      </div>
    </el-dialog>
  </section>
</template>
<script>
import { formMixins } from '@/components/mixins/formMixins'
import { orgApis } from '@/api/caseManagementQuery'

export default {
  components: {
    dialogHeader: () => import('@/components/dialogHeader'),
    mixButton: () => import('@/components/basicComponent/mixButton')
  },

  // 组件混入对象
  mixins: [formMixins],

  props: {
    popupsVisible: { type: Boolean, default: false },
    popupsState: { type: String, default: '' },
    // 从网格传入的编辑行数据(json)
    dynamicEditRowData: { type: Object, default: function() { return {} } }
  },

  data() {
    const viewPageCode = 'bigAreaInfo-edit'
    return {
      reclaimUnitCode: '',
      unitAddress: '',
      // 动态组件-按钮
      tableButtons: [
        {
          compKey: 'btnKey1',
          type: 'primary',
          size: 'small',
          icon: 'iconfont  icon-preservation',
          clickEvent: () => this.editSave(),
          disabled: true,
          text: '提交',
          position: 'right'
        },
        {
          compKey: 'btnKey2',
          size: 'small',
          disabled: true,
          icon: 'iconfont  icon-cancel',
          clickEvent: () => this.closeEdit(),
          text: this.$t('sys.button.cancel') // 取消
        }
      ],

      // 动态组件-查询条件
      tableComponents:
        this.$ConfigCache.CacheConfig.cacheData[viewPageCode] &&
        this.$ConfigCache.CacheConfig.cacheData[viewPageCode].formComponents.length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[viewPageCode].formComponents
          : [
            {
              compKey: 'comKey1',
              labelName: '门店编码',
              codeField: 'dlrCode',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMust: true,
              isRequire: false,
              disabled: true,
              span: 8
            },
            {
              compKey: 'compKey2',
              labelName: '门店名称',
              codeField: 'dlrFullName',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMust: true,
              disabled: true,
              span: 8
            },
            {
              compKey: 'compKey3',
              labelName: '产品类别',
              codeField: 'batterySpecies',
              customObj: {
                api: orgApis.productType,
                defOptionFields: ['LOOKUP_VALUE_CODE', 'LOOKUP_VALUE_NAME']
              },
              component: () => import('@/components/org/commonSelect'),
              type: 'dropdownList',
              isMust: true,
              disabled: false,
              span: 8
            },
            {
              compKey: 'compKey4',
              labelName: '电池编码',
              codeField: 'batteryCode',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMust: true,
              disabled: false,
              span: 8
            },
            {
              compKey: 'compKey5',
              labelName: '回收服务网点名称',
              codeField: 'unitId',
              textField: 'reclaimUnitName',
              type: 'inputText',
              disabled: false,
              component: () => import('@/views/ve/planReport/components/multipleSearchInput'),
              dataToObject: {
                // 控制弹窗页面 搜索 网格 元素
                tableComponents: [
                  {
                    compKey: 'compKey6',
                    labelName: '回收服务网点名称：',
                    codeField: 'reclaimUnitName',
                    component: () => import('@/components/org/commonInput'),
                    type: 'inputText',
                    isMust: true,
                    span: 8
                  },
                  {
                    compKey: 'compKey7',
                    labelName: '地址：',
                    codeField: 'unitAddress',
                    component: () => import('@/components/org/commonInput'),
                    type: 'inputText',
                    isMust: true,
                    span: 8
                  }
                ],
                tableCols: [
                  {
                    prop: 'reclaimUnitName',
                    label: '回收服务网点名称',
                    align: 'center'
                  },
                  {
                    prop: 'reclaimUnitCode',
                    label: '回收服务网点统一社会信用代码',
                    align: 'center'
                  },
                  {
                    prop: 'unitAddress',
                    label: '详细地址',
                    align: 'center'
                  }
                ],
                formField: {
                  reclaimUnitName: '',
                  unitAddress: ''
                },
                callbackObj: {
                  onRowClick: (row, column, event) => {
                    this.formField1.billOrderId = row
                  }
                },
                oFields: 'unitId,reclaimUnitName,unitAddress,reclaimUnitCode',
                returnField: [
                  {
                    prop: 'unitId',
                    label: '回收服务网点ID'
                  },
                  {
                    prop: 'reclaimUnitCode',
                    label: '回收服务网点ID'
                  }
                ],
                api: orgApis.recoveryOutletsDelList
              },
              span: 8,
              isMust: true
            },
            {
              compKey: 'compKey8',
              labelName: '回收服务网点统一社会信用代码',
              codeField: 'reclaimUnitCode',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              disabled: true,
              isMust: true,
              span: 8
            },
            {
              compKey: 'compKey9',
              labelName: '入库日期',
              codeField: 'putinDate',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMust: true,
              hidden: true,
              span: 8
            },
            {
              compKey: 'compKey10',
              labelName: '回收服务网点详细地址',
              codeField: 'unitAddress',
              component: () => import('@/components/org/commonInput'),
              disabled: true,
              type: 'inputText',
              isMust: true,
              span: 24
            }
          ],

      // 标题
      textMap: {
        edit: '回收网点入库信息详情',
        add: '回收网点入库信息新增'
      },

      // 表单数据（无需赋值，由混入对象赋值）
      formField: {
        dlrCode: '',
        dlrFullName: '',
        batterySpecies: '',
        batteryCode: '',
        reclaimUnitName: '',
        reclaimUnitCode: '',
        putinDate: '',
        unitAddress: '',
        unitId: ''
      }
    }
  },

  methods: {
    closeEdit() {
      this.curPopupsVisible = false
      this.popupsState = ''
      this.$emit('close', false)
    },

    getComponentCode1(val, modelText, codeField, comType, popupsKey, textField, otherField, clickRowData) {
      this.getComponentCode(val, modelText, codeField, comType, popupsKey, textField, otherField, clickRowData)
      const results = clickRowData

      if (results.reclaimUnitCode) {
        this.formField.reclaimUnitCode = results.reclaimUnitCode
      } else {
        this.formField.reclaimUnitCode = this.formField.reclaimUnitCode
      }
      if (results.unitAddress) {
        this.formField.unitAddress = results.unitAddress
      } else {
        this.formField.unitAddress = this.formField.unitAddress
      }
    },

    open() {
      if (this.popupsState === 'add') {
        this.sentry()
        this.tableComponents[6] = {}
        this.tableComponents[2].disabled = false
        this.tableComponents[3].disabled = false
        this.tableComponents[4].disabled = false
        this.tableButtons = [
          {
            compKey: 'btnKey1',
            type: 'primary',
            size: 'small',
            icon: 'iconfont  icon-preservation',
            clickEvent: () => this.editSave(),
            disabled: true,
            text: '提交',
            position: 'right'
          },
          {
            compKey: 'btnKey2',
            type: '',
            size: 'small',
            disabled: true,
            icon: 'iconfont  icon-cancel',
            clickEvent: () => this.closeEdit(),
            text: this.$t('sys.button.cancel') // 取消
          }
        ]
      } else if (this.popupsState === 'edit') {
        this.tableComponents[2].disabled = true
        this.tableComponents[3].disabled = true
        this.tableComponents[4].disabled = true
        this.tableButtons = []
        this.tableComponents[6] = {
          compKey: 'compKey9',
          labelName: '入库日期',
          codeField: 'putinDate',
          component: () => import('@/components/org/commonInput'),
          type: 'inputText',
          isMust: true,
          hidden: true,
          disabled: true,
          span: 8
        }
      }
    },

    // 查询门店编码，门店名称
    sentry() {
      const queryObj = {
        type: 'mutation',
        // api配置
        apiConfig: orgApis.queryDlr,
        // 条件/实体参数（变量），根据typeParam中的定义配置
        variables: this.formField
      }
      var paramD = this.$getParams(queryObj)
      this.$requestAPI(paramD).then(response => {
        if (response.result === '1') {
          this.formField.dlrCode = response.rows[0].dlrCode
          this.formField.dlrFullName = response.rows[0].dlrName
        }
      })
    },

    // 保存
    editSave() {
      const info = {}
      info.batterySpecies = this.formField.batterySpecies
      info.batteryCode = this.formField.batteryCode
      info.unitId = this.formField.unitId
      const queryObj = {
        type: 'mutation',
        // api配置
        apiConfig: orgApis.recoveryOutletsAdd,
        // 条件/实体参数（变量），根据typeParam中的定义配置
        variables: this.formField
      }
      // 转换了中台请求格式数据
      var paramD = this.$getParams(queryObj)
      // 调用中台API方法（可复用）
      this.$requestAPI(paramD).then(response => {
        if (response.result === '1') {
          this.$message({
            message: response.msg /* 保存成功*/,
            type: 'success',
            duration: 1000
          })
          this.curPopupsVisible = false
          this.$nextTick(() => {
            this.$parent.queryTable(1)
          })
        } else {
          this.$message({
            message: response.msg,
            type: 'warn',
            uration: 1000
          })
        }
      })
    }
  }

}
</script>

<style lang="scss" scoped>
.promptSpan {
  position: absolute;
  bottom: 26px;
  right: 335px;
  color: #e70020;
}
</style>

